﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Events;
using Scoreboard.Basement.Events;
using Scoreboard.Basement;
using Scoreboard.Basement.Controls;
using Scoreboard.Basement.Views;

namespace Scoreboard.Main
{
	public partial class Shell : UserControl
	{
		IDictionary<DialogBase, DialogWindow> _dialogWindows = new Dictionary<DialogBase, DialogWindow>();

		public Shell(ShellViewModel shellViewModel)
		{
			InitializeComponent();

			ViewModel = shellViewModel;

			var eventAggregator = ServiceLocator.Get<IEventAggregator>();
			var showDialogEvent = eventAggregator.GetEvent<ShowDialogEvent>();
			showDialogEvent.Subscribe(ShowDialogEventHandler);
		}

		public ShellViewModel ViewModel 
		{
			get
			{
				return DataContext as ShellViewModel;
			}
			set
			{
				DataContext = value;
			}
		}		

		public void ShowDialogEventHandler(ShowDialogEventArgs eventArgs)
		{
			DialogWindow dialogWindow;

			if (eventArgs.NeedsCaching)
			{
				if (_dialogWindows.ContainsKey(eventArgs.Dialog))
				{
					dialogWindow = _dialogWindows[eventArgs.Dialog];
				}
				else
				{
					dialogWindow = new DialogWindow(eventArgs.Dialog);				

					_dialogWindows.Add(eventArgs.Dialog, dialogWindow);
				}

				/*double screenWidth = App.Current.Host.Content.ActualWidth;
				double screenHeight = App.Current.Host.Content.ActualHeight;

				dialogWindow.Width = screenWidth * 0.8;
				dialogWindow.Height = screenHeight * 0.8;*/
			}
			else
			{
				dialogWindow = new DialogWindow(eventArgs.Dialog);
			}

			dialogWindow.Show();					
		}
	}
}
